package meterMonitor.service;

import meterMonitor.constant.Constant;
import meterMonitor.framework.context.CurrentUser;
import meterMonitor.framework.context.SessionContext;
import meterMonitor.mapper.AlertMapper;
import meterMonitor.model.AlertRecPO;
import meterMonitor.model.bo.RealtimeAlertRecBO;
import meterMonitor.model.vo.CallParameter;
import meterMonitor.model.vo.StatusVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;

import static meterMonitor.constant.Constant.*;

@Service
public class AlertService {
    @Autowired
    AlertMapper alertMapper;

    public List<AlertRecPO> getAlertRec(AlertRecPO alertRec) {
        return alertMapper.getAlertRec(alertRec);
    }

    public int addAlertRec(AlertRecPO alertRec) {

        return alertMapper.addAlertRec(alertRec);
    }

    /**
     * 处理异常记录
     *
     * @param alertRec the alert rec
     * @return the int
     */
    public int operatingAlert(AlertRecPO alertRec) {
        Timestamp time2 = new Timestamp(new Date().getTime());
        alertRec.setDealedTime(time2);
        return alertMapper.operatingAlert(alertRec);
    }

    /**
     * 异常详情
     *
     * @param parameter
     */
    @Deprecated
    public Map<Object, Object> getAlertTypeRec(CallParameter parameter) {

        int notPr = 0;
        int unPro = 0;
        int processing = 0;
        Map<Object, Object> map = new HashMap<>();
        // if (parameter.getStartTime() == null) {
        //     parameter.setStartTime(DateUtil.monthFirstday());
        // }
        // if (parameter.getEndTime() == null) {
        //     parameter.setEndTime(DateUtil.monthLastday());
        // }

        //获取本月某类型设备总异常条数
        List<RealtimeAlertRecBO> recBOS = alertMapper.getAlertTypeRec(parameter);
        //获取具体设备的总条数
        Map<String, List<RealtimeAlertRecBO>> grouping = recBOS.stream().collect(Collectors.groupingBy(RealtimeAlertRecBO::getDevName));

        for (RealtimeAlertRecBO alertRecBO : recBOS) {
            map.put(alertRecBO.getDevName(), grouping.get(alertRecBO.getDevName()).size());

            if (alertRecBO.getDealState() == DEAL_WAITED) {
                //未处理
                notPr++;
            } else if (alertRecBO.getDealState() == DEAL_PROCESSED) {
                //已处理
                unPro++;
            } else if (alertRecBO.getDealState() == DEAL_PROCESSING) {
                //处理中
                processing++;
            }

        }

        map.put("notPr", notPr);
        map.put("unPro", unPro);
        map.put("processing", processing);
        map.put("count", recBOS.size());
        map.put("grouping", grouping);
        return map;
    }

    public List<StatusVO> paramsStatus() {
        List<StatusVO> statusList = alertMapper.paramsStatus();
        CurrentUser currentUser = SessionContext.getCurrentUser();
        if (currentUser.getRoleId() == Constant.ROLE_GUEST) {
            for (StatusVO status : statusList) {
                status.setTypeCount(0);
            }
        }
        return statusList;
    }

    public Map<String, List<StatusVO>> alertsStatus() {
        List<StatusVO> status = alertMapper.alertsStatus();
        Map<String, List<StatusVO>> map = new HashMap<>();
        for (StatusVO s : status) {
            boolean contains = map.keySet().contains(s.getLineName());
            if (contains) {
                map.get(s.getLineName()).add(s);
            } else {
                List<StatusVO> list = new ArrayList<>();
                list.add(s);
                map.put(s.getLineName(), list);
            }
        }
        return map;
    }

    public List<AlertRecPO> alertsInfo() {
        return alertMapper.alertsInfo();
    }

    public List<AlertRecPO> queryHistory(CallParameter callParameter) {
        return alertMapper.queryHistory(callParameter);
    }
}
